package com.common.zrd.validation;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * author: zrd
 * @Date: 2019/11/4 17:32
 * @Description:
 */
public class FrameworkStringUtils {
    private static final String PREFIX = "\\u";
    private static final Pattern INT_PATTERN = Pattern.compile("^\\d+$");

    private FrameworkStringUtils() {
    }

    public static boolean isEmpty(Object str) {
        return str == null || "".equals(str);
    }

    public static boolean hasLength(CharSequence str) {
        return str != null && str.length() > 0;
    }

    public static boolean hasLength(String str) {
        return hasLength((CharSequence) str);
    }

    public static boolean hasText(CharSequence str) {
        if (!hasLength(str)) {
            return false;
        } else {
            int strLen = str.length();

            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean hasText(String str) {
        return hasText((CharSequence) str);
    }

    public static boolean isInteger(String str) {
        return str != null && str.length() != 0 && INT_PATTERN.matcher(str).matches();
    }

    public static String convertBool(String value) {
        if ("true".equalsIgnoreCase(value)) {
            return "1";
        } else {
            return "false".equalsIgnoreCase(value) ? "0" : value;
        }
    }

    public static boolean getBool(String value) {
        return value != null && !"false".equalsIgnoreCase(value) && !"0".equals(value) && !value.isEmpty();
    }

    public static String replaceAll(String string, String oldString, String newString) {
        return innerReplace(string, oldString, newString, true);
    }

    public static String replaceFirst(String string, String oldString, String newString) {
        return innerReplace(string, oldString, newString, false);
    }

    private static String innerReplace(String string, String oldString, String newString, boolean isAll) {
        int index = string.indexOf(oldString);
        if (index == -1) {
            return string;
        } else {
            int start = 0;
            int len = oldString.length();
            if (len == 0) {
                return string;
            } else {
                StringBuilder buffer = new StringBuilder(string.length());

                do {
                    buffer.append(string, start, index);
                    buffer.append(newString);
                    start = index + len;
                    if (!isAll) {
                        break;
                    }

                    index = string.indexOf(oldString, start);
                } while (index != -1);

                buffer.append(string.substring(start));
                return buffer.toString();
            }
        }
    }

    public static String concat(String... string) {
        int length = 0;
        String[] var2 = string;
        int var3 = string.length;

        int var4;
        for (var4 = 0; var4 < var3; ++var4) {
            String str = var2[var4];
            length += str.length();
        }

        StringBuilder buf = new StringBuilder(length);
        String[] var8 = string;
        var4 = string.length;

        for (int var9 = 0; var9 < var4; ++var9) {
            String str = var8[var9];
            buf.append(str);
        }

        return buf.toString();
    }

    public static String joinMap(Map<String, String> maps) {
        if (maps != null && !maps.isEmpty()) {
            StringBuilder builder = new StringBuilder();
            Iterator var2 = maps.entrySet().iterator();

            while (var2.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry) var2.next();
                builder.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
            }

            if (builder.length() > 0) {
                builder.setLength(builder.length() - 1);
            }

            return builder.toString();
        } else {
            return "";
        }
    }

    public static Map<String, String> splitToMap(String value) {
        if (!hasLength(value)) {
            return Collections.emptyMap();
        } else {
            Map<String, String> maps = new HashMap(16);
            String[] var2 = value.split(",");
            int var3 = var2.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                String kav = var2[var4];
                String[] kavv = kav.split("=");
                switch (kavv.length) {
                    case 0:
                        break;
                    case 1:
                        maps.put(kavv[0], "");
                        break;
                    case 2:
                        maps.put(kavv[0], kavv[1]);
                        break;
                    default:
                        throw new IllegalArgumentException("Value (" + value + ") is Invalid.");
                }
            }

            return maps;
        }
    }

    public static boolean isBooleanValue(String str) {
        return Boolean.TRUE.toString().equalsIgnoreCase(str) || Boolean.FALSE.toString().equalsIgnoreCase(str);
    }

    public static boolean isIntValue(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException var2) {
            return false;
        }
    }

    public static boolean isLongValue(String str) {
        try {
            Long.parseLong(str);
            return true;
        } catch (NumberFormatException var2) {
            return false;
        }
    }

    public static boolean isDecimalDigits(String decimal) {
        return isDecimalDigits(decimal, 2);
    }

    public static boolean isDecimalDigits(String decimal, int scale) {
        BigDecimal decimalValue;
        try {
            decimalValue = new BigDecimal(decimal);
        } catch (NumberFormatException var4) {
            return false;
        }

        return decimalValue.scale() <= scale;
    }

    public static String join(String[] arrays, String delimiters) {
        if (arrays == null) {
            return "";
        } else if (arrays.length == 1) {
            return arrays[0];
        } else {
            int length = arrays.length;
            StringBuilder buffer = new StringBuilder(arrays[0]);

            for (int i = 1; i < length; ++i) {
                buffer.append(delimiters).append(arrays[i]);
            }

            return buffer.toString();
        }
    }

    public static String join(Collection<String> arrays, String delimiters) {
        if (arrays == null) {
            return "";
        } else if (arrays.size() == 1) {
            return arrays.iterator().next();
        } else {
            Iterator<String> iterator = arrays.iterator();
            StringBuilder buffer = new StringBuilder(iterator.next());

            while (iterator.hasNext()) {
                buffer.append(delimiters).append(iterator.next());
            }

            return buffer.toString();
        }
    }

    public static String formatNormalDate(Date date) {
        return isEmpty(date) ? "null" : (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS")).format(date);
    }

    public static boolean equals(String input1, String input2) {
        if (input1 == input2) {
            return true;
        } else {
            boolean hasLength1 = hasLength(input1);
            boolean hasLength2 = hasLength(input2);
            if (!hasLength1 && !hasLength2) {
                return true;
            } else {
                return (hasLength1 && hasLength2) && input1.equals(input2);
            }
        }
    }

    public static boolean equalsIgnoreCase(String input1, String input2) {
        if (input1 == input2) {
            return true;
        } else {
            boolean hasLength1 = hasLength(input1);
            boolean hasLength2 = hasLength(input2);
            if (!hasLength1 && !hasLength2) {
                return true;
            } else {
                return (hasLength1 && hasLength2) && input1.equalsIgnoreCase(input2);
            }
        }
    }

    public static boolean isEmpty(String input) {
        return input == null || input.trim().length() == 0;
    }

    public static boolean isNotEmpty(String input) {
        return !isEmpty(input);
    }

    public static String safeTrim(String text) {
        return text == null ? null : text.trim();
    }

    public static String safeToString(Object input) {
        return input == null ? null : input.toString();
    }

    public static String defaultValue(String text, String defaultValue) {
        text = safeTrim(text);
        return isNotEmpty(text) ? text : defaultValue;
    }

    public static int defaultValue(String text, int defaultValue) {
        text = safeTrim(text);
        if (isNotEmpty(text)) {
            try {
                return Integer.parseInt(text);
            } catch (NumberFormatException var3) {
            }
        }

        return defaultValue;
    }

    public static boolean defaultValue(String text, boolean defaultValue) {
        text = safeTrim(text);
        return !isNotEmpty(text) || !Boolean.TRUE.toString().equalsIgnoreCase(text) && !Boolean.FALSE.toString().equalsIgnoreCase(text) ? defaultValue : Boolean.parseBoolean(text);
    }

    public static boolean isContains(String[] values, String value) {
        if (value != null && value.length() > 0 && values != null && values.length > 0) {
            String[] var2 = values;
            int var3 = values.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                String v = var2[var4];
                if (value.equals(v)) {
                    return true;
                }
            }
        }

        return false;
    }

    public static String native2Ascii(String str) {
        char[] charArray = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        char[] var3 = charArray;
        int var4 = charArray.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            char c = var3[var5];
            sb.append(char2Ascii(c));
        }

        return sb.toString();
    }

    public static String char2Ascii(char c) {
        if (c > 255) {
            StringBuffer sb = new StringBuffer();
            sb.append("\\u");
            int code = c >> 8;
            String tmp = Integer.toHexString(code);
            if (tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
            code = c & 255;
            tmp = Integer.toHexString(code);
            if (tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
            return sb.toString();
        } else {
            return Character.toString(c);
        }
    }

    public static String acsii2Native(String str) {
        StringBuffer sb = new StringBuffer();
        int begin = 0;

        for (int index = str.indexOf("\\u"); index != -1; index = str.indexOf("\\u", begin)) {
            sb.append(str, begin, index);
            sb.append(ascii2Char(str.substring(index, index + 6)));
            begin = index + 6;
        }

        sb.append(str.substring(begin));
        return sb.toString();
    }

    private static char ascii2Char(String str) {
        if (str.length() != 6) {
            throw new IllegalArgumentException("Ascii string of a native character must be 6 character.");
        } else if (!"\\u".equals(str.substring(0, 2))) {
            throw new IllegalArgumentException("Ascii string of a native character must start with \"\\u\".");
        } else {
            String tmp = str.substring(2, 4);
            int code = Integer.parseInt(tmp, 16) << 8;
            tmp = str.substring(4, 6);
            code += Integer.parseInt(tmp, 16);
            return (char) code;
        }
    }
}
